<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\api\model\Performance as PerformanceModel;
use app\api\model\ProjectPerformanceClass;
use app\api\model\ProjectPerformanceSpot;
use app\api\model\ProjectPerOrg as ProjectPerOrgModel;
use app\api\model\ProjectPerOrgPeople as ProjectPerOrgPeopleModel;
use app\api\model\ProjectReviewResult as ProjectReviewResultModel;
use app\api\model\ProjectLockReviewResult as ProjectLockReviewResultModel;
use app\Request;

class Performance extends Base
{

    /**
     * 绩效考核分类列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function performanceClassList(Request $request)
    {
        try {

            $page = $request->param('page');
            $pagesize = $request->param('pagesize');
            $PerformanceModel = new PerformanceModel;
            $list = $PerformanceModel->alias('p')
                ->field('*')
                ->paginate([
                    'list_rows'=> $pagesize,
                    'var_page' => 'page',
                    'page'=>$page
                ]);
            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 添加绩效考核分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function performanceAdd(Request $request){
        try {
            if($request->param('name'))  $data['name'] = $request->param('name');
            if($request->param('mark'))  $data['mark'] = $request->param('mark');
            if($request->param('sort'))  $data['sort'] = $request->param('sort');
            $data['create_time'] = time();

            $PerformanceModel = new PerformanceModel();
            $performanceClass = $PerformanceModel->where('name','LIKE',$data['name'])->find();
            if($performanceClass){
                $result = ["status"=>false,"msg"=>"分类已存在！"];
                //return json($result);
            }

            $res = $PerformanceModel->insertGetId($data);
            if($res){
                $result = ["status"=>true,"msg"=>"添加成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"添加失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 编辑考核绩效分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function performanceSave(Request $request){
        try {
            $id = $request->param('id');
            if($request->param('name'))  $data['name'] = $request->param('name');
            if($request->param('mark'))  $data['mark'] = $request->param('mark');
            if($request->param('sort'))  $data['sort'] = $request->param('sort');
            $data['update_time'] = time();

            $PerformanceModel = new PerformanceModel();
            $performanceClass = $PerformanceModel->where('name','LIKE',$data['name'])->find();
            if($performanceClass){
                $result = ["status"=>false,"msg"=>"分类已存在！"];
                //return json($result);
            }

            $res = $PerformanceModel->save($data);
            if($res !== false){
                $result = ["status"=>true,"msg"=>"编辑成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"编辑失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 删除考核绩效分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function delPerformanceClass(Request $request){
        try {

            $id = $request->param('id');
            $PerformanceModel = new PerformanceModel();
            $res = $PerformanceModel->where('id','=',$id)->delete();
            if($res){
                $result = ["status"=>true,"msg"=>"删除成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"删除失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 获取绩效考核单生成情况
     * @param Request $request
     * @return \think\response\Json
     */
    public function getPerformanceCreateList(Request $request){
        try {

            $page = (int)$request->param('page');
            $pagesize = (int)$request->param('pagesize');
            $ProjectReviewResultModel = new ProjectReviewResultModel;
            $total = $ProjectReviewResultModel
                ->field('count(distinct month) as total')
                ->order('month desc')
                ->find();
            $list = $ProjectReviewResultModel->distinct(true)
                ->field('month')
                ->page($page,$pagesize)
                ->order('month desc')
                ->select();
            foreach ($list as $v){
                $v['month'] = date('Y-m-d',$v['month']);
            }
            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>["data"=>$list,"total"=>$total["total"],"per_page"=>$pagesize,"current_page"=>$page]];

            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 获取绩效考核锁定列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function getPerformanceLockList(Request $request){
        try {

            $page = (int)$request->param('page');
            $pagesize = (int)$request->param('pagesize');
            $ProjectLockReviewResultModel = new ProjectLockReviewResultModel;
            $total =  $ProjectLockReviewResultModel
                ->field('count(distinct month) as total')
                ->order('month desc')
                ->find();
            $list = $ProjectLockReviewResultModel->distinct(true)
                ->field('month')
                ->page($page,$pagesize)
                ->order('month desc')
                ->select();
            foreach ($list as $v){
                $v['month'] = date('Y-m-d',$v['month']);
            }
            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>["data"=>$list,"total"=>$total["total"],"per_page"=>$pagesize,"current_page"=>$page]];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 获取考核标准预览列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function getPerformanceListdisplay(Request $request){
        try {
            global $ReviewResult;
            $this->showReviewResultIteraction(0);
            $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$ReviewResult];
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }


    /**
     * 迭代查询
     * @param int $orgid
     * @param string $month
     * @return \think\response\Json
     */
    private  function  showReviewResultIteraction($orgid=0){
        try {

            global $ReviewResult;
            //查询所有的考核点
            $ProjectPerformanceSpotModel = new ProjectPerformanceSpot();
            $spotListInfo = $ProjectPerformanceSpotModel
                ->alias('s')
                ->field('s.id,s.name,o.orgname')
                ->leftjoin('ProjectPerOrg o','s.orgid = o.id')
                ->where('s.status','=',1)
                ->where('s.pid','<>',0)
                ->order('s.orgid asc')
                ->select()->toArray();
            foreach ($spotListInfo as $key=>$rv){
                $spotListInfo[$key]['name'] = html_entity_decode($rv['name']);
            }
            $sql = $ProjectPerformanceSpotModel->getLastSql();
            if($spotListInfo){
                $ReviewResult[] = $spotListInfo;
            }

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
}
